/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.pathfinder;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
import pt.iasd.youthshelter.datamodel.service.ChurchController;

@ActionID(
        category = "File/New",
        id = "pt.iasd.youthshelter.pathfinder.NewPathfinderEditorAction"
)
@ActionRegistration(
        displayName = "#CTL_NewPathfinderEditorAction"
)
@ActionReference(path = "Menu/File/New"/*, position = 3333*/)
@Messages("CTL_NewPathfinderEditorAction=Pathfinder")
public final class NewPathfinderEditorAction implements ActionListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/pathfinder/Bundle");
    
    @Override
    public void actionPerformed(ActionEvent e) {
        ChurchController controller = Lookup.getDefault().lookup(ChurchController.class);
        if(controller != null) {
            if(controller.getChurchCount() > 0) {
                NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine(
                        bundle.getString("Input.Pathfinder.Name"),
                        bundle.getString("Input.Pathfinder.Title")
                );
                if(DialogDisplayer.getDefault().notify(input) == NotifyDescriptor.OK_OPTION
                        && input.getInputText() != null && !input.getInputText().isEmpty()) {
                    TopComponent editor = new PathfinderEditorTopComponent(input.getInputText());
                    editor.open();
                    editor.requestActive();
                }
            } else {
                NotifyDescriptor dialog = new NotifyDescriptor(bundle.getString("NoChurchSelected.Error")
                            .replace("\n", System.getProperty("line.separator")),
                        bundle.getString("NoChurch.Title"), NotifyDescriptor.ERROR_MESSAGE,
                        NotifyDescriptor.ERROR_MESSAGE, null, null);
                DialogDisplayer.getDefault().notify(dialog);
            }
        }
    }
}
